﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YJKRevitRoom.NumberRoomControler
{
	public static class ElementParameter
	{
		public static bool SetParameterValue(Element element, string name, object value)
		{
			if (element == null || string.IsNullOrEmpty(name) || value == null)
			{
				return false;
			}
			bool result;
			try
			{
				Parameter parameter = element.GetParameter(name);
				if (parameter == null)
				{
					result = false;
				}
				else
				{
					bool flag;
                    switch ((int)parameter.StorageType)
					{
					case 1:
						flag = parameter.Set(Convert.ToInt32(value));
						goto IL_BC;
					case 2:
					{
						double num = UnitConvert.CovertToAPI(Convert.ToDouble(value), parameter.DisplayUnitType);
						flag = parameter.Set(num);
						goto IL_BC;
					}
					case 4:
					{
						ElementId elementId;
						if (value.GetType() == typeof(ElementId))
						{
							elementId = (value as ElementId);
						}
						else
						{
							elementId = new ElementId(Convert.ToInt32(value));
						}
						flag = parameter.Set(elementId);
						goto IL_BC;
					}
					}
					flag = parameter.Set(value.ToString());
					IL_BC:
					result = flag;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

        public static bool SetParameterValueEx(Element element, string name, object value, ParameterType type = (ParameterType)1)
		{
			if (element == null || string.IsNullOrEmpty(name) || value == null)
			{
				return false;
			}
			bool result;
			try
			{
				if (element.GetParameter(name) == null)
				{
                    BuiltInCategory integerValue = (BuiltInCategory)element.Category.Id.IntegerValue;
					Dictionary<string, ParameterType> dictionary = new Dictionary<string, ParameterType>();
					dictionary.Add(name, type);
					ShareParameterOperator.InsertSharedParameter(element.Document, integerValue, "房间编号数据", dictionary, true);
				}
				result = ElementParameter.SetParameterValue(element, name, value);
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}
	}
}
